/*
 * Copyright 2002-2016 the Beijing Brain Technology co.,LTD http://www.bris.cn/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bris.niosocket.mapping;

import java.lang.reflect.Method;
import java.util.Comparator;
import java.util.Set;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import com.bris.niosocket.annotation.Path;
import com.bris.niosocket.annotation.SocketController;
import com.bris.niosocket.message.IMessage;

/**
 * socket请求方法级别映射</br>
 * @see SocketController
 * 2016年2月17日</br>
 * @author xunaidong
 * @since 1.0.0
 */
@Component
public class RequestHandlerMapping extends AbstractHandlerMethodMapping {

	/**
	 * 持有{@link SocketController}注解的bean被作为请求的处理的handler</br>
	 * @see SocketController
	 */
	@Override
	protected boolean isHandler(Class<?> beanType) {
		// TODO Auto-generated method stub
		return AnnotationUtils.findAnnotation(beanType, SocketController.class)!=null;
	}

	/* (non-Javadoc)
	 * @see com.bris.niosocket.mapping.AbstractHandlerMethodMapping#getMappingForMethod(java.lang.reflect.Method, java.lang.Class)
	 */
	@Override
	protected String getMappingForMethod(Method method, Class<?> handlerType) {
		String path = null;
		Path pathAnnotation = AnnotationUtils.findAnnotation(method, Path.class);
		if(pathAnnotation!=null){
			path = pathAnnotation.value();//获取注解的值
		}
		return path;
	}

	/* (non-Javadoc)
	 * @see com.bris.niosocket.mapping.AbstractHandlerMethodMapping#getMappingPathPatterns(java.lang.Object)
	 */
	@Override
	protected Set<String> getMappingPathPatterns(String mapping) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.bris.niosocket.mapping.AbstractHandlerMethodMapping#getMatchingMapping(java.lang.Object, com.bris.niosocket.message.IMessage)
	 */
	@Override
	protected String getMatchingMapping(String mapping, IMessage request) {
		if(mapping.equalsIgnoreCase(request.getPath()))//字符串对比
			return request.getPath();
		return null;
	}

	/* (non-Javadoc)
	 * @see com.bris.niosocket.mapping.AbstractHandlerMethodMapping#getMappingComparator(com.bris.niosocket.message.IMessage)
	 */
	@Override
	protected Comparator<String> getMappingComparator(IMessage request) {
		// TODO Auto-generated method stub
		return null;
	}



}
